import util.ListNode;

/**
 * Created by losor on 2017/3/13.
 */
public class _21_MergeTwoSortedLists {

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        mergeTwoLists(a,b);

    }

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }
        else if(l2 == null){
            return l1;
        }
        ListNode out = l1.val<l2.val?l1:l2;
        ListNode temp;
        ListNode old  = out;
        if (l1.val<l2.val){
            l1 = l1.next;
        }
        else{
            l2 = l2.next;
        }
        while(l1 != null && l2 != null){
            if (l1.val<l2.val){
                old.next = l1;
                old = old.next;
                l1 = l1.next;
            }
            else{
                old.next = l2;
                old = old.next;
                l2 = l2.next;
            }
        }
        if (l1 == null){
            old.next = l2;
        }
        else{
            old.next = l1;
        }
        return out;

    }
}
